<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar">
        <!-- 这里是状态栏 -->
      </view>
      <view class="jtop_l" @tap="navigateBack">
        <image src="../../static/img/ic_back.png"></image>
      </view>
      <!-- <view class="jtop_l" @tap="navigateBack">关闭</view> -->
      <view class="jtop_c">上传合同</view>
      <view
        class="jtop_r"
        :style="{ opacity: contractName ? '1' : '.6' }"
        @click="jsave"
        >确定</view
      >
    </view>
    <view class="tophr"></view>

    <view class="addb">
      <view class="addi2">
        <text>合同名称</text>
        <input
          type="text"
          placeholder="请输入合同名称"
          :value="contractName"
          @input="onKeyInput_contractName"
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <view class="addi2">
        <text>合同编号</text>
        <input
          type="text"
          placeholder="请输入合同编号"
          :value="contractNo"
          @input="onKeyInput_contractNo"
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <view class="addi2 noborder">
        <text>合同标签</text>

        <!-- <input
          type="text"
          placeholder="请输入合同标签"
          :value="tag"
          @input="onKeyInput_tag"
          placeholder-style="color:#CCCCCC"
        /> -->

        <biner-input-tags
          class="addi2_in"
          @change="change"
          :selectlist="selectlist"
          :disabled="disabled"
          :value="tag"
          :placeholder="placeholder"
        ></biner-input-tags>
      </view>
    </view>

    <!-- @tap="jshow_pop" -->

    <view class="addi">
      <text>拍照上传</text>
      <view class="att">
        <view v-for="(item, index) in jimg" :key="item.id">
          <image class="at" :src="item" @longpress="logoTime(index)"></image>
        </view>

        <image
          src="../../static/img/index/new/at.png"
          mode=""
          class="at"
          @tap="changeAvatar()"
        ></image>
      </view>
    </view>

    <!-- 弹出层 -->
    <view :class="[ashowpop ? 'tc' : 'tc hide']">
      <view class="jtop_bj" @tap="jshow_pop"></view>

      <view class="tc_fbox">
        <view class="tc_fboxs">
          <view class="tc_fbox_ic">
            <image src="../../static/img/doc/ic_pic.png"></image>
            <text>上传照片</text>
          </view>
          <view class="tc_fbox_ic">
            <image src="../../static/img/doc/ic_file.png"></image>
            <text>上传文件</text>
          </view>
        </view>
        <view class="tc_fbox_bt" @tap="jshow_pop">
          取消
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState } from 'vuex';
import binerInputTags from '@/components/biner-input-tags/biner-input-tags';

export default {
  components: {
    binerInputTags,
  },
  data() {
    return {
      repeat_click: true,
      ashowpop: false,
      ashowpop_jrename: false,
      ashowpop_jdel: false,
      ashowpopT: false,
      bj: false,
      yd: false,

      contractNo: '',
      fileId: '',
      tag: '',
      jimg: [],
      fileList: [],

      disabled: false, //不禁用
      // 默认的数组 不填默认的是空数组
      selectlist: [],
      // 默认提示
      placeholder: '请输入合同标签',
      contractName: '',
    };
  },

  methods: {
    change(arr) {
      this.tag = JSON.stringify(arr);
      console.log(this.tag);
    },
    //图片上传
    btTC_jdel() {
      var ax = this.jimg;
      ax.splice(this.scdtp, 1);
      console.log('ax1111', ax);
      this.jimg = ax;
      console.log('ax222', this.jimg);
      this.ashowpop2 = false;
    },

    btTCC(e) {
      this.ashowpop2 = !this.ashowpop2;
    },

    logoTime(e) {
      console.log(e);
      this.scdtp = e;
      this.ashowpop2 = true;
    },

    onLoad(e) {
      this.folderId = e.folderId;
    },

    conver(limit) {
      var size = '';
      //   if (limit < 0.1 * 1024) {
      //     //如果小于0.1KB转化成B
      //     size = limit.toFixed(2) + 'B';
      //   } else if (limit < 0.1 * 1024 * 1024) {
      //如果小于0.1MB转化成KB
      size = (limit / 1024).toFixed(2);
      //   } else if (limit < 0.1 * 1024 * 1024 * 1024) {
      //     //如果小于0.1GB转化成MB
      //     size = (limit / (1024 * 1024)).toFixed(2) + 'MB';
      //   } else {
      //     //其他转化成GB
      //     size = (limit / (1024 * 1024 * 1024)).toFixed(2) + 'GB';
      //   }

      var sizestr = size + '';
      var len = sizestr.indexOf('\.');
      var dec = sizestr.substr(len + 1, 2);
      if (dec == '00') {
        //当小数点后为00时 去掉小数部分
        return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
      }
      return sizestr;
    },

    changeAvatar() {
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera', 'album'], //从相册选择、摄像头
        success: function(res) {
          console.log(
            'jjjjj:',
            res,
            JSON.stringify(res.tempFilePaths, res.tempFiles[0]),
          );

          var asize = that.conver(res.tempFiles[0].size);
          var aname = res.tempFiles[0].name;
          var str;
          var ipos;
          str = res.tempFiles[0].type;
          ipos = str.indexOf('/');

          var str2 = str.substring(ipos, str.length); //取后zhuan部shu分

          that.sctp(res.tempFilePaths, res.tempFiles[0], str2, asize, aname);
        },
      });
    },

    sctp(a, b, c, d, e) {
      console.log(a);
      var als = '';
      var tpbt = b.name;
      if (tpbt == undefined || tpbt == 'undefined') {
        tpbt = b.path;
      }
      var pos = tpbt.lastIndexOf('.');
      var suffix = tpbt.substring(pos);

      function random_string(len) {
        len = len || 32;
        var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
          pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
      }
      var g_object_name = random_string(10) + suffix;

      // 获取信息
      request({
        url: '/biz/oss/policy',
        method: 'GET',

        success: res => {
          if (res.data.success == true) {
            als = res.data.data;
            var lssj = {
              capacity: d,
              fileName: e,
              fileUrl: '',
              suffix: c.replace('/', ''),
            };
            request({
              filePath: a[0],
              oss: true,
              formData: {
                name: tpbt,
                key: als.dir + g_object_name,
                policy: als.policy,
                OSSAccessKeyId: als.accessid,
                signature: als.signature,
                callback: als.callback,
                success_action_status: '200',
              },

              success: res => {
                var cc = JSON.parse(res.data);
                console.log('上传图片', cc);
                if (cc.code == '00000') {
                  var dd = this.fileList;
                  var ddimg = this.jimg;

                  var ci = cc.data.url + '/' + als.dir + g_object_name;

                  lssj.fileUrl = ci;
                  dd.push(lssj);
                  this.fileList = dd;

                  ddimg.push(ci);
                  this.jimg = ddimg;

                  console.log(this.fileList);
                } else {
                  this.repeat_click = true;
                  uni.showToast({
                    title: cc.message,
                    icon: 'none',
                  });
                }
              },
            });
          }
        },
      });
    },

    //--图片上传

    // 监听变化的数据

    onKeyInput_contractName: function(event, id) {
      this.contractName = event.detail.value;
    },
    onKeyInput_tag: function(event, id) {
      this.tag = event.detail.value;
    },

    onKeyInput_contractNo: function(event, id) {
      this.contractNo = event.detail.value;
    },

    jsave() {
      console.log('img:', this.jimg);

      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;

      if (!this.contractName) {
        this.repeat_click = true;
        uni.showToast({
          title: '请输入合同名称',
          icon: 'none',
        });
        return;
      }

      console.log('xxxxxx:', this.fileList);

      if (this.fileList.length == 0) {
        this.repeat_click = true;
        uni.showToast({
          title: '请上传文件',
          icon: 'none',
        });
        return;
      }

      // var jd = {
      //   name: this.name,
      // };

      request({
        url: `/biz/contract`,
        data: {
          contractNo: this.contractNo,
          folderId: this.folderId,
          tag: this.tag,
          contractName: this.contractName,
          fileList: this.fileList,
        },

        method: 'POST',
        success: openIdRes => {
          console.log(openIdRes);
          if (openIdRes.data.success == true) {
            var that = this;

            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1000,
              success: function() {
                setTimeout(function() {
                  uni.navigateBack();
                }, 1500);
              },
            });

            // uni.navigateBack()
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    // 跳转
    jshow_pop() {
      this.ashowpop = !this.ashowpop;
    },
    btTC2() {
      uni.navigateTo({
        url: '../main/work_add',
      });
    },

    navigateBack() {
      uni.navigateBack();
    },
  },
  computed: mapState(['forcedLogin', 'hasLogin', 'userName']),
};
</script>

<style>
.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: none;
}

.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}

.tc_box {
  width: 540rpx;
  height: auto;
  background: #ffffff;
  border-radius: 14rpx;
  margin: 460rpx 105rpx 0;
  position: relative;
  z-index: 10001;
  overflow: hidden;
}

.tc_boxt {
  width: 100%;
  height: 44rpx;
  font-size: 36rpx;
  line-height: 44rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 40rpx 0 20rpx;
  color: #000000;
}

.tc_boxtc {
  width: 100%;
  height: 36rpx;
  font-size: 28rpx;
  line-height: 36rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 0rpx 0 30rpx;
  color: #666666;
}

.tc_boxi {
  width: 460rpx;
  height: 88rpx;
  background: #ffffff;
  border-radius: 8rpx;
  border: 1px solid #dddddd;
  display: block;
  float: left;
  margin: 0rpx 40rpx 22rpx 40rpx;
}

.tc_boxi input {
  width: 90%;
  height: 48rpx;
  margin: 20rpx 5%;
  font-size: 32rpx;
  color: #000000;
}

.tc_f {
  width: 100%;
  height: 100rpx;
  display: block;
  float: left;
  border-top: 1px solid #dcdee3;
  margin: 20rpx 0 0;
}

.tc_fl {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  border-right: 1px solid #dcdee3;
}

.tc_fr {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  opacity: 0.5;
}

.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}

.tc_fboxs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}

.tc_fbox_ic {
  width: 33.3333%;
  display: block;
  float: left;
  text-align: center;
  font-size: 28rpx;
  color: #666666;
}

.tc_fbox_ic image {
  width: 72rpx;
  height: 72rpx;
  display: block;
  float: none;
  margin: 60rpx auto 24rpx;
}

.tc_fbox_ic text {
  width: 100%;
  height: auto;
  display: block;
  float: none;
  margin: 0 auto 64rpx;
}

.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
}

.tc_list {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}

.tc_list_t {
  width: 100%;
  height: 80rpx;
  display: block;
  float: left;
  background: #ffffff;
  font-size: 24rpx;
  color: #999999;
  line-height: 80rpx;
  margin: 0rpx 0;
  border-bottom: 1px solid #dddddd;
  text-align: center;
}

.tc_list {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  border-bottom: 1px solid #dddddd;
}

.tc_list1 {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 36rpx;
  color: #000000;
  line-height: 44rpx;
  margin: 34rpx 28rpx 38rpx 30rpx;
}

.tc_listtwo {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 28rpx;
  color: #999999;
  line-height: 36rpx;
  margin: 42rpx 0rpx 0rpx 0rpx;
}

.tc_list image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 36rpx 30rpx 0 0;
}

.tophr {
  width: 100%;
  height: 88rpx;
}

.uni-searchbar {
  box-shadow: none;
}

.at {
  width: 160rpx;
  height: 160rpx;
  display: block;
  float: left;
  margin: 0rpx 0 0 0;
}

.att {
  width: 720rpx;
  margin: 0 0 30rpx 30rpx;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}

.addb_1 {
  width: 720rpx;
  margin: 20rpx 30rpx 20rpx 30rpx;
  font-size: 32rpx;
  line-height: 4r0px;
  color: #000000;
  height: auto;
  display: block;
  float: left;
}

.addb_2 {
  width: 720rpx;
  margin: 0rpx 30rpx 20rpx 30rpx;
  font-size: 32rpx;
  line-height: 4r0px;
  color: #666666;
  height: auto;
  display: block;
  float: left;
}

.ptt {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}

.pttl {
  width: 84rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 20rpx 18rpx 30rpx;
}

.pttr {
  width: 400rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 0rpx 0rpx 0rpx;
}

.pttrt {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 34rpx;
  color: #000000;
}

.pttrf {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #999999;
}

.pttjt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 48rpx 30rpx 0 30rpx;
}

.ptf {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}

.ptft {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
}

.ptft image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 21rpx 20rpx 0 0rpx;
}

.ptft text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 21rpx 20rpx 0 0rpx;
  font-size: 34rpx;
  line-height: 48rpx;
  color: #000000;
}

.addi2 {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}

.addi {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  box-shadow: inset 0px 1px 0px #dddddd, inset 0px -1px 0px #dddddd;
  margin: 20rpx 0 0;
}

.addi2 text {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 0rpx;
}

.addi text {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 30rpx;
}

.addi input,
.addi2 input {
  width: auto;
  height: 50rpx;
  display: block;
  float: left;
  margin: 20rpx 30rpx 0 0;
  text-align: left;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
  border: none;
  padding: 0;
}

.addi2_in {
  width: 490rpx;
  min-height: 50rpx;
  height: auto;
  display: block;
  float: right;
  margin: 25rpx 30rpx 20rpx 0;
  text-align: left;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
  border: none;
  padding: 0;
}

.input-tag .u-tag {
  float: left;
  width: auto;
}

.noborder {
  height: auto;
}

.addi2_in input,
.addi2_in div {
  font-size: 34rpx;
  color: #cccccc;
}

.addi2_in div {
  margin-bottom: 20rpx;
}

.uni-input-input {
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
}

.addb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}

.addbi {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}

.addb .addbi:last-child {
  border: none;
}

.addbi .addbil {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
}

.addbi .addbir {
  width: auto;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 32rpx;
  color: #000000;
}

.addbi image {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 33rpx 30rpx 0 30rpx;
}

.addi textarea {
  width: 690rpx;
  height: 150rpx;
  display: block;
  float: left;
  margin: 0rpx 30rpx 20rpx;
  text-align: left;
  line-height: 40rpx;
  font-size: 34rpx;
  color: #000000;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}

.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}

.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}

.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}

.jtop_lt {
  margin: 0 0 0 10rpx;
}

.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}

.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}

.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}

.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}

.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}

.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}

.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
  opacity: 0.5;
}

.doc_b22 {
  opacity: 0.5;
}

.nd {
  width: 735rpx;
  height: auto;
  display: block;
  float: left;
  margin: 0 0 0 0rpx;
}

.nd_bt {
  width: auto;
  height: 48rpx;
  display: block;
  float: right;
  border: 1px solid #dddddd;
  border-radius: 24rpx;
  margin: 40rpx 0 40rpx 30rpx;
}

.nd_bt text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 0 7rpx 0 18rpx;
  line-height: 48rpx;
  color: #666666;
  font-size: 24rpx;
}

.nd_bt image {
  width: 13rpx;
  height: 8rpx;
  display: block;
  float: left;
  margin: 21rpx 14rpx 0 0;
}

.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: block;
}

.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}

.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
  background: #ffffff;
}

.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #f2f2f2;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}

.tc_fbox_bt2 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 0rpx solid #f2f2f2;
  background: #ffffff;
}

.tc_fbox_bt3 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-bottom: 1px solid #dddddd;
  background: #ffffff;
}
</style>
